package cn.jinhe.doc.view.action;

import java.util.Date;
import java.util.List;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;

import cn.jinhe.doc.base.BaseAction;
import cn.jinhe.doc.domain.MaterialConsume;
import cn.jinhe.doc.domain.Service;
import cn.jinhe.doc.domain.ServiceTopic;
import cn.jinhe.doc.domain.User;

@Controller
@Scope("prototype")
public class ServiceTopicAction extends BaseAction<ServiceTopic> {
	private Long serviceId;
	
	private Long serviceTopicId;
	
	public Long getServiceTopicId() {
		return serviceTopicId;
	}



	public void setServiceTopicId(Long serviceTopicId) {
		this.serviceTopicId = serviceTopicId;
	}

	private Long userId; 
	public String show() throws Exception {
		// 准备数据:topic
		
		
		ServiceTopic serviceTopic = serviceTopicService.getById(model.getId());
		ActionContext.getContext().put("serviceTopic", serviceTopic);
		
		List<MaterialConsume> materialConsumeList=materialConsumeService.findByServiceTopic(serviceTopic);
		ActionContext.getContext().put("materialConsumeList", materialConsumeList);

		return "show";

	}

	
	public String delete() throws Exception{
		serviceTopicService.delete(serviceTopicId);
		return "toList";
		
	}
	public String addUI() throws Exception {
		Service service = serviceService.getById(serviceId);
		ActionContext.getContext().put("service", service);	
		List<User> userList=userService.findAll();
		ActionContext.getContext().put("userList", userList);
		return "addUI";
	}

	public String add() throws Exception {
		// 表单中参数传递过来,已经封装了
		// model.setTitle(title);
		// model.setContent(content);
		// model.setService(repositoryService.getById(serviceId));
		model.setUser(userService.getById(userId));
		model.setService(serviceService.getById(serviceId));
		model.setRecorder(getCurrentUser());// 当前登录用户
		model.setIpAddr(ServletActionContext.getRequest().getRemoteAddr());// 当前请求中的IP地址
		model.setPostTime(new Date());// 当前时间
		model.setUser(userService.getById(userId));
		serviceTopicService.save(model);
		return "toShow";
	}

	public Long getServiceId() {
		return serviceId;
	}

	public void setServiceId(Long serviceId) {
		this.serviceId = serviceId;
	}

	public Long getUserId() {
		return userId;
	}

	public void setUserId(Long userId) {
		this.userId = userId;
	}

}
